$(document).ready(function () {
    getUserName();
    getCount();
    getInventory();
    getitemerror();
    getCreateItemWorning();
})

function getInventory() {
    axios.get('/Inventory/GetInventory')
        .then(function (result) {
            $("#content-block1").html(result.data);
        });
}

function getUserName(){
    axios.get('/Layout/GetUserName')
        .then(function (userName) {
            document.getElementById("UserName").innerHTML = userName.data;
        });
}

function getCount() {
    axios.get('/Layout/GetCount')
        .then(function (Count) {
            document.getElementById("InventoryCount").innerHTML = Count.data[0];
            document.getElementById("finishItemCount").innerHTML = Count.data[1];
            document.getElementById("OverdueItemCount").innerHTML = Count.data[2];
            document.getElementById("notFinishItemCount").innerHTML = Count.data[3];
        });
}

function viewCreateInventory() {
    axios.get('/Inventory/ViewCreateInventory')
        .then(function (result) {
            //$("#content-block1").html(result.data);
            $("#newblock1").html(result.data);
        });
}

function showInventoryItems(inventoryId) {
    showNoFinishItems(inventoryId);
    showFinishItems(inventoryId);
    showOverdueItems(inventoryId);
    getCount()
}

function showNoFinishItems(inventoryId) {
    axios.get('/Item/ShowNoFinishItems', {
        params: {
            inventoryId: inventoryId
        }
    })
        .then(function (result) {
            $("#content-block2").html(result.data);
        });
}

function showFinishItems(inventoryId) {
    axios.get('/Item/ShowFinishItems', {
        params: {
            inventoryId: inventoryId
        }
    })
        .then(function (result) {
            $("#content-block3").html(result.data);
        });
}

function showOverdueItems(inventoryId) {
    axios.get('/Item/ShowOverdueItems', {
        params: {
            inventoryId: inventoryId
        }
    })
        .then(function (result) {
            $("#content-block4").html(result.data);
        });
}

function AfterViewCreateInventory() {
    var code = event.keyCode;
    if (code == 13) {
        inventoryName = $("#create-inventory-name").val();
        axios.post('/Inventory/CreateInventory',
            $.param({
                inventoryname: inventoryName
            })
        )
            .then(function (result) {
                $("#content-block1").html(result.data);
                getCount()
            });
    }
}

function DeleteInventory() {
    var inventoryId = $("#inventoryId").val();
    axios.post('/Inventory/RemoveInventory',
        $.param({
            inventoryId: inventoryId
        })
    )
        .then(function (result) {
            $(".modal-backdrop").css("display", "none");
            $("#content-block1").html(result.data);
            getCount();
            showInventoryItems(inventoryId)
        });
}

function DeleteItem() {
    var inventoryId = $("#inventoryId").val();
    var itemId = $("#itemId").val();
    axios.post('/Item/DeleteItemeInInventory',
        $.param({
            itemId: itemId,
            inventoryId: inventoryId
        })
    )
        .then(function (result) {
            $(".modal-backdrop").css("display", "none");
            $("#deleteItemInInventoryModel").css("display", "none");
            getCount();
            showInventoryItems(inventoryId)
        });
}

function deleteInSearch() {
    var itemId = $("#itemId").val();
    axios.get('/Item/DeleteItemInSearch', {
        params:{
            itemId: itemId,
        }
    })
        .then(function (result) {
            $(".modal-backdrop").css("display", "none");
            $("#findItem-delete").css("display", "none");
            $("#Reasult").html(result.data);
            getCount()
        });
}

function ChangeStatue(itemId,inventoryId) {
    axios.post('/Item/ChangeStatueInInventory',
        $.param({
            inventoryId: inventoryId,
            itemId: itemId
        })
    )
        .then(function (param) {
            showNoFinishItems(inventoryId);
            showFinishItems(inventoryId);
            getCount()
        });
}

function getitemerror() {

    $("#item-content").blur(function () {
        var name = $("#item-content").val();
        if (name == "") {
            $("#content-error").html("内容不能为空!");
            return;
        }
        else if (name != "") {
            var a = $("#content-error").text();
            $("#content-error").html("")
            return;
        }
    })
    $("#item-content").keypress(function () {
        var a = $("#content-error").text();
        $("#content-error").html("请输入50字以内。");
        return;
    })
    $("#item-mark").blur(function () {
        var name = $("#item-mark").val();
        if (name == "") {
            $("#mark-error").html("内容不能为空!");
            return;
        }
        else if (name != "") {
            var a = $("#mark-error").text();
            $("#mark-error").html("")
            return;
        }
    })
    $("#item-mark").keypress(function () {
        var a = $("#content-error").text();
        $("#mark-error").html("请输入50字以内。");
        return;
    })

}

function getCreateItemWorning() {
    $("#item-create-content").blur(function () {
        var name = $("#item-create-content").val();
        if (name == "") {
            $("#create-content-warning").html("内容不能为空!");
            return;
        }
        else if (name != "") {
            $("#create-content-warning").html("")
            return;
        }
    })
    $("#item-create-content").keypress(function () {
        $("#create-content-warning").html("请输入50字以内。");
        return;
    })
    $("#item-create-mark").blur(function () {
        var name = $("#item-create-mark").val();
        if (name == "") {
            $("#create-mark-warning").html("内容不能为空!");
            return;
        }
        else if (name != "") {

            $("#create-mark-warning").html("")
            return;
        }
    })
    $("#item-create-mark").keypress(function () {
        var a = $("#create-mark-warning").text();
        $("#create-mark-warning").html("请输入50字以内。");
        return;
    })
}